package com.example.mybubbleshooter;

import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainThread extends Thread {

	// flag to hold game state
	private boolean running;

	public PlayView playv;
	public SurfaceHolder holder;

	public MainThread(SurfaceHolder hol, PlayView game) {
		holder = hol;
		playv = game;
	}

	public void setRunning(boolean running) {
		this.running = running;
	}

	@SuppressLint("WrongCall")
	@Override
	public void run() {
		// long tickCount = 0L;
		// Log.d("thread", "Starting game loop");
		// while (running) {
		// tickCount++;
		// Canvas c;
		//
		// c = holder.lockCanvas(null);
		//
		// synchronized (holder) {
		//
		// playv.postInvalidate();
		// }
		// // update game state
		// // try {
		// // //playv.gameLoop();
		// // } catch (InterruptedException e) {
		// // // TODO Auto-generated catch block
		// // e.printStackTrace();
		// // }
		// // render state to the screen
		// try {
		// sleep(5000);
		// } catch (InterruptedException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// Log.d("thread", "Game loop executed " + tickCount + " times");
		// }

		Canvas c;

		while (running) {

			c = null;

			try {

				c = holder.lockCanvas(null);

				synchronized (holder) {
					//playv.invalidate();
					
					playv.updateBallMove();
					playv.checkColletion();
					 playv.updateTime();
					 playv.onDraw(c);
					
				}

			} finally {

				// do this in a finally so that if an exception is thrown

				// during the above, we don't leave the Surface in an

				// inconsistent state

				if (c != null) {

					holder.unlockCanvasAndPost(c);

				}

			}
//			try {
//				sleep(20);
//			} catch (InterruptedException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
		}

	}
}
